package com.icinfo.cloud.provider.common.crontask;

import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.CaseDocumentRecordQueryDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseDocumentRecordMapper;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.mapper.CaseDocumentAuthedEntrustMapper;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.mapper.CaseDocumentSendinfoConfirmMapper;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.model.CaseDocumentAuthedEntrust;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.model.CaseDocumentSendinfoConfirm;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description 文书失效状态更新定时任务
 * @Author zhengqiang
 * @Date 2022/5/24 10:58
 **/
//@Component
public class DocumentStatusCheckTask {

    @Resource
    private CaseDocumentRecordMapper caseDocumentRecordMapper;
    @Resource
    private CaseDocumentAuthedEntrustMapper caseDocumentAuthedEntrustMapper;
    @Resource
    private CaseDocumentSendinfoConfirmMapper caseDocumentSendinfoConfirmMapper;

    /**
     * 送达方式及地址确认书失效状态更新任务
     */
    //@Scheduled(cron = "0 0 1 * * ?")
    public void docSendInfoConfirmStatusUpdateTask(){
        //送达方式及地址确认书  从完成确认时间开始计算  有效期一年（365日）
        List<CaseDocumentRecordQueryDto> caseDocumentRecordQueryDtos = caseDocumentRecordMapper.selectDocSendInfoConfirmOfInvalid();
        caseDocumentRecordQueryDtos.forEach(caseDocumentRecord -> {
            //更新超过一年的送达方式及地址确认书为失效状态
            Example updateExample = new Example(CaseDocumentSendinfoConfirm.class);
            updateExample.createCriteria().andEqualTo("caseId", caseDocumentRecord.getCaseId()).andEqualTo("partyId", caseDocumentRecord.getPartyId());
            CaseDocumentSendinfoConfirm caseDocumentSendinfoConfirm = new CaseDocumentSendinfoConfirm();
            caseDocumentSendinfoConfirm.setStatus(Constant.INTEGER_0);
            caseDocumentSendinfoConfirmMapper.updateByExampleSelective(caseDocumentSendinfoConfirm, updateExample);
        });


    }

    /**
     * 授权委托书信息表失效状态更新任务
     */
    //@Scheduled(cron = "0 0 1 * * ?")
    public void docAuthedEntrustStatusUpdateTask(){
        //授权委托书 委托期限：到本案终结为止
        //获取前一天结案对应的授权委托书记录
        List<CaseDocumentRecordQueryDto> caseDocumentRecordQueryDtos = caseDocumentRecordMapper.selectDocAuthedEntrustOfYesterdayEndCase();
        caseDocumentRecordQueryDtos.forEach(caseDocumentRecord -> {
            //更新案件终结的授权委托书为失效状态
            Example updateExample = new Example(CaseDocumentAuthedEntrust.class);
            updateExample.createCriteria().andEqualTo("caseId", caseDocumentRecord.getCaseId()).andEqualTo("partyId", caseDocumentRecord.getPartyId());
            CaseDocumentAuthedEntrust caseDocumentAuthedEntrust = new CaseDocumentAuthedEntrust();
            caseDocumentAuthedEntrust.setStatus(Constant.INTEGER_0);
            caseDocumentAuthedEntrustMapper.updateByExampleSelective(caseDocumentAuthedEntrust, updateExample);
        });

    }
}
